package com.neusoft.yiyang.medical.controller;

import com.neusoft.yiyang.common.entity.medical.MedicationOrder;
import com.neusoft.yiyang.medical.service.MedicationOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "药物医嘱管理接口", description = "负责药物医嘱的创建、查询、更新及删除，关联药品信息与客户用药记录")
@RestController
@RequestMapping("/medication-orders")
public class MedicationOrderController {

    @Autowired
    private MedicationOrderService medicationOrderService;

    @Operation(summary = "创建药物医嘱", description = "为客户开具药物医嘱（含药品、剂量、用法、频次等信息）")
    @PostMapping("/create")
    public void createMedicationOrder(
            @Parameter(description = "药物医嘱信息（含客户ID、药品ID、用药方案等）", required = true)
            @RequestBody MedicationOrder medicationOrder) {
        medicationOrderService.createMedicationOrder(medicationOrder);
    }

    @Operation(summary = "获取所有药物医嘱", description = "查询系统中所有的药物医嘱记录")
    @GetMapping("/all")
    public List<MedicationOrder> getAllMedicationOrders() {
        return medicationOrderService.getAllMedicationOrders();
    }

    @Operation(summary = "查询客户的药物医嘱", description = "获取指定客户的所有药物医嘱记录")
    @GetMapping("/customer/{customerId}")
    public List<MedicationOrder> getMedicationOrdersByCustomerId(
            @Parameter(description = "客户ID", required = true)
            @PathVariable Integer customerId) {
        return medicationOrderService.getMedicationOrdersByCustomerId(customerId);
    }

    @Operation(summary = "更新药物医嘱", description = "修改药物医嘱的信息（如调整剂量、停药等）")
    @PutMapping("/update/{medicationOrderId}")
    public void updateMedicationOrder(
            @Parameter(description = "药物医嘱ID", required = true) @PathVariable Integer medicationOrderId,
            @Parameter(description = "更新后的药物医嘱信息", required = true) @RequestBody MedicationOrder medicationOrder) {
        medicationOrder.setMedicationOrderId(medicationOrderId);
        medicationOrderService.updateMedicationOrder(medicationOrder);
    }

    @Operation(summary = "删除药物医嘱", description = "移除指定的药物医嘱记录")
    @DeleteMapping("/delete/{medicationOrderId}")
    public void deleteMedicationOrder(
            @Parameter(description = "药物医嘱ID", required = true) @PathVariable Integer medicationOrderId) {
        medicationOrderService.deleteMedicationOrder(medicationOrderId);
    }

    @Operation(summary = "查询护理任务关联的药物医嘱", description = "通过药物医嘱ID获取对应的药物信息（用于护理任务关联）")
    @GetMapping("/getByMedicationOrderId/{medicationOrderId}")
    public MedicationOrder getByMedicationOrderId(
            @Parameter(description = "药物医嘱ID", required = true) @PathVariable Integer medicationOrderId) {
        return medicationOrderService.getByMedicalRecordId(medicationOrderId);
    }

    @Operation(summary = "查询客户的药物医嘱及药品详情", description = "获取指定客户的药物医嘱及关联的药品信息（如药品名称、规格等）")
    @GetMapping("/getMedicationByCustomerId/{customerId}")
    public List<Map<String, Object>> getMedicationByCustomerId(
            @Parameter(description = "客户ID", required = true) @PathVariable Integer customerId) {
        return medicationOrderService.getMedicationByCustomerId(customerId);
    }
}